
#include "cv.h"
#include "highgui.h"

#include "Histogram.h"
#include "Frame.h"
#include "Video.h"
#include "Transition.h"
#include "Cut.h"
#include "Test.h"

#include <iostream>
using namespace std;

void main(int argc, char *argv[])
{
    Video *vdo;
    double userThreshold = 0;
    int vrType = 0;
    
    //vdo = new Video("f:\\WORKSPACE\\OPENCV\\data\\indi002.avi");
    
    if(argc == 4)
    {
        userThreshold = atof(argv[1]);
        vrType = atoi(argv[2]);
        vdo = new Video(argv[3]);
    }
    else
    {
        cout << "Usage: VisualRhythm.exe [threshold] [type] [videofile]" << endl;
        cout << "   + threshold: 0->100: percentage of frame height, used to count for cut point" << endl;
        cout << "   + type: 0 -> visual rhythm by subsampling, 1 -> visual rhythm by histogram" << endl;
        cout << "   + videofile: path to your video file" << endl;

        exit(1);
    }
    
    cout << "H-W-FPS-T:  " << vdo->getFramesHeight() << "-" << vdo->getFramesWidth() << "-" <<
        vdo->getFPS() << "-" << vdo->getFramesTotal() << endl;
    unsigned int dc = (unsigned int) vdo->getCodec();
    cout << "FOURCC: " << (char)dc << "-" << (char)(dc >> 8) << "-" << (char)(dc >> 16) << "-" << (char)(dc >> 24) << endl;

    Cut* DTC = new Cut();
    DTC->setUserThreshold(userThreshold);
    DTC->setVRType(vrType);

    //Fade* DTF = new Fade();
    //Dissolve* DTD = new Dissolve();
    //Test * DTT = new Test();

    //long posFrame = (long)vdo->getCurrentPosition();
    //cout << posFrame << endl;

    vdo->seekFrame(0);
    
    std::vector<Transition> transList;
    
    cout << "Starting scene cut detection..." << endl;
    clock_t start_t, end_t;

    // capture start time
    start_t = clock();

    DTC->detectTransitions(vdo, &transList);
    
    // capture end time
    end_t = clock();

    char name[32];

    int duration = end_t - start_t;
    sprintf(name, "%02d:%02d:%03d", duration/1000/60, duration/1000%60, duration%1000);
    cout << "Duration: (mm:ss:ms)" << name << endl;

    cout << "Actual threshold: " << DTC->getThreshold() << endl;
    cout << "Number of scene cuts: " << transList.size() << endl;

    std::vector<Transition>::iterator iter;
    
    cout << "Writing scene-cut frames..." << endl;

    for(iter = transList.begin(); iter != transList.end(); iter++)
    {
        long pos = iter->getPosTransition();
        cout <<  pos << endl;

        vdo->seekFrame(pos);
        Frame * tf = vdo->getCurrentFrame();
        long msec = vdo->getCurrentPosMsec();
       
        sprintf(name, "Frame %06d - at %02d-%02d-%03d.jpg", pos, msec/1000/60, msec/1000%60, msec % 1000);
       
        /*
        cvNamedWindow( name, 1);
        cvFlip(tf->data, NULL, 0);
        cvShowImage( name, tf->data );

        while(cvWaitKey(1) != 'c');
        */
 
        cvFlip(tf->data, NULL, 0); // flip the frame
        tf->write(name);
    }

    cvWaitKey();
}